// MyClass.cs created with MonoDevelop
// User: Zaiden at 10:31 09/12/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace ZPod.MTG
{
	/// <summary>
	/// Carta del juego
	/// </summary>
	[DataContract]
	[Serializable]
	public class Card
	{
		/// <summary>
		/// Nombre de la carta
		/// </summary>
		[DataMember]
		public string Name
		{
			get;
			set;
		}

		/// <summary>
		/// Comentario de la carta
		/// </summary>
		[DataMember]
		public String Comment
		{
			get;
			set;
		}

		[DataMember]
		private List<Card> _attachedCards = new List<Card>();

		/// <summary>
		/// Lista de cartas asociadas
		/// </summary>
		public List<Card> AttachedCards
		{
			get
			{
				return new List<Card>(_attachedCards);
			}
		}

		/// <summary>
		/// Crea una carta especifica
		/// </summary>
		/// <param name="name"></param>
		public Card(string name)
		{
			Name = name;
		}


		/// <summary>
		/// Crea una carta generica
		/// </summary>
		public Card()
		{
			Name = String.Empty;
		}

		/// <summary>
		/// Asocia una carta a la carta actual
		/// </summary>
		/// <param name="card"></param>
		public void Attach(Card card)
		{
			_attachedCards.Add(card);
		}

		/// <summary>
		/// Asocia un set de cartas a la carta actual
		/// </summary>
		/// <param name="cards"></param>
		public void AttachRange(IEnumerable<Card> cards)
		{
			_attachedCards.AddRange(cards);
		}

		/// <summary>
		/// Limpia las cartas asociadas
		/// </summary>
		public void DetachAll()
		{
			_attachedCards.Clear();
		}

		/// <summary>
		/// Desasocia una carta
		/// </summary>
		/// <param name="card"></param>
		public void Detach(Card card)
		{
			Detach(card.Name);
		}

		/// <summary>
		/// Desasocia una carta, segun su nombre
		/// </summary>
		/// <param name="name"></param>
		public void Detach(String name)
		{
			for (int i = 0; i < _attachedCards.Count; i++)
			{
				if (_attachedCards[i].Name == name)
				{
					Detach(i);
					break;
				}
			}
		}

		/// <summary>
		/// Desasocia una carta, segun el indice en la lista de cartas asociadas
		/// </summary>
		/// <param name="pos"></param>
		public void Detach(int pos)
		{
			_attachedCards.RemoveAt(pos);
		}

		public override bool Equals(object obj)
		{
			Card card = obj as Card;
			if (obj == null || 
				card.Name != Name || 
				card._attachedCards.Count != _attachedCards.Count)
			{
				return false;
			}

			for (int i = 0; i < _attachedCards.Count; i++)
			{
				if (_attachedCards[i] != card._attachedCards[i])
				{
					return false;
				}
			}

			return true;
		}

		public override int GetHashCode()
		{
			return this.Name.GetHashCode();
		}
	}
}
